defmodule Pango.IpLib do
    @moduledoc false

    def get_local_ip() do
        case :os.type() do
            {:unix, :linux} ->
                Cmd.run("ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d \"addr:\"")
                |> hd()
            _ ->
                {:ok, ips} = :inet.getifaddrs()
                [{_, opts}] = Enum.filter(
                    ips,
                    fn ({_, opts}) ->
                        opts[:flags] == [:up, :running]
                    end
                )
                [addr: ip] = Enum.filter(
                    opts,
                    fn ({key, value}) ->
                        key == :addr && tuple_size(value) == 4
                    end
                )
                Tuple.to_list(ip)
                |> Enum.map_join(".", &(&1))
        end
    end

end
